package org.elec.mac.controller;

import lombok.extern.slf4j.Slf4j;
import org.elec.mac.ehcache.CacheConfiguration;
import org.elec.mac.ehcache.ManageEhCache;
import org.elec.mac.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Slf4j
@Controller
public class MyController {

    @Autowired
    private ManageEhCache manageEhCache;

    @Autowired
    private CacheConfiguration cacheConfiguration;

    @Value("${tencent.wxpay.myorder}")
    private String myOrderUrl;

    @Value("${tencent.wx.appid}")
    private String appid;

    @RequestMapping(value = "/zhuanpan.do")
    private String zhuanpan(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException {
        String openid = request.getParameter("openid");
        String accesstoken = request.getParameter("accesstoken");
        String mach = request.getParameter("mach");
        String action = request.getParameter("action");

        //Map<String, String> mapSign = Utils.wxConfigMap(myOrderUrl);

        model.put("accesstoken", accesstoken);
        model.put("mach", mach);
        model.put("openid", openid);
        model.put("appid", appid);
        //model.put("noncestr", mapSign.get("noncestr"));
        //model.put("timestamp", mapSign.get("timestamp"));
        //model.put("signature", mapSign.get("sign"));
        return "luckdraw";
    }

    @RequestMapping(value = "/my.do")
    private String myHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException {
        String openid = request.getParameter("openid");
        String accesstoken = request.getParameter("accesstoken");
        String mach = request.getParameter("mach");
        model.put("openid", openid);
        model.put("accesstoken", accesstoken);
        model.put("mach", mach);
        return "my";
    }

    @RequestMapping(value = "/myorder.do")
    private String orderHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException {
        String openid = request.getParameter("openid");
        String accesstoken = request.getParameter("accesstoken");
        String mach = request.getParameter("mach");
        String action = request.getParameter("action");

        Map<String, String> mapSign = Utils.wxConfigMap(myOrderUrl);

        model.put("accesstoken", accesstoken);
        model.put("mach", mach);
        model.put("openid", openid);
        model.put("appid", appid);
        model.put("noncestr", mapSign.get("noncestr"));
        model.put("timestamp", mapSign.get("timestamp"));
        model.put("signature", mapSign.get("sign"));
        return "myorder";
    }

}
